package com.pzhu.servlet;

import com.pzhu.pojo.Book;
import com.pzhu.service.BookService;
import com.pzhu.service.impl.BookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class AddBooksServlet extends HttpServlet {
    BookService bs = new BookServiceImpl();

    @Override
//	add
    protected void service(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        String[] name = req.getParameter("name").split(",|，");
        String[] price = req.getParameter("price").split(",|，");
        String[] author = req.getParameter("author").split(",|，");
        String[] piblisher = req.getParameter("publisher").split(",|，");
        int i = 0;
        while (i < name.length) {
            Book book = new Book();
            book.setName(name[i]);
            book.setPrice(Double.parseDouble(price[i]));
            book.setAuthor(author[i]);
            book.setPublisher(piblisher[i]);
            bs.addBook(book);
            i++;
        }
        resp.sendRedirect("/jdbc/show");
        return;

    }
}
